﻿using System;
using System.Text; 
using System.Globalization;
using System.Web.UI;
using System.Collections.Generic;
 
using SiteUnity.Frame.Common.Tools.Class;
using SiteUnity.Layers.Application;
using SiteUnity.Layers.Application.Files;
using SiteUnity.Layers.Entity.General;
using SiteUnity.Layers.Entity.PageItems.Files;
using SiteUnity.Layers.Application.Contents;
using SiteUnity.Frame.Common;
using SiteUnity.Frame.Common.UITools;
using SiteUnity.Layers.Application.BaseClasses;
using SiteUnity.Frame.Modules.Gallery.Slider;
using SiteUnity.Frame.Common.Tools;

namespace SiteUnity.Frame.Modules
{
    [ToolboxData("<{0}:PageGallery runat=server>")]
    public class PageGallery : BaseWebControl
    {
        private SiteUnity.Layers.Entity.General.Page _currentPage { get; set; }

        public List<PageItem> Pages { get; set; }
        public View View { get; set; }
        public string PageUrl { get; set; }
        public int ItemCount { get; set; }
        public bool ShowHeader { get; set; }
        public string HeaderText { get; set; }
        public string Target { get; set; }

        private bool hasPages = true;

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            if (Pages == null && string.IsNullOrEmpty(PageUrl))
                Pages = convertPageItem(SiteManager.Website.CurrentPage.ChildPages);
            else if (!string.IsNullOrEmpty(PageUrl))
            {
                _currentPage = SiteManager.Website.SelectPageByUrl(PageUrl);
                Pages = convertPageItem(_currentPage.ChildPages);
            }

            if (Pages.Count == 0)
            {
                hasPages = false;
                return;
            }

            if (string.IsNullOrEmpty(HeaderText))
                HeaderText = _currentPage.Title;

            if (string.IsNullOrEmpty(Target))
                Target = "_self";
        }

        private List<PageItem> convertPageItem(List<SiteUnity.Layers.Entity.General.Page> pages)
        {
            List<PageItem> pageitems = new List<PageItem>();

            foreach (SiteUnity.Layers.Entity.General.Page page in pages)
                pageitems.Add(new PageItem(page, page.Url));

            return pageitems;
        }

        private void generateGridView(HtmlTextWriter output)
        {
            output.AddAttribute(HtmlTextWriterAttribute.Class, View.ToString());
            output.AddAttribute(HtmlTextWriterAttribute.Id, this.ID);
            output.RenderBeginTag(HtmlTextWriterTag.Ul);

            int i = 1;
            foreach (PageItem page in Pages)
            {
                if (ItemCount != 0 && i - 1 == ItemCount)
                    break;

                if (string.IsNullOrEmpty(page.ImageUrl))
                {
                    List<File> pageImages = FileManager.getFileByPage(page);

                    File img_217x145 = null;
                    foreach (File image in pageImages)
                    {
                        if (image.TypeID == FileType.img_217x145)
                        {
                            img_217x145 = image;
                            break;
                        }
                    }

                    page.ImageUrl = img_217x145 != null ? img_217x145.Url : string.Empty;
                }

                string itemID = "id" + i;
                string itemTitle = page.Title;
                string itemUrl = page.Url;
                string itemImgUrl = page.ImageUrl;
                string itemCategory = ClassCollection.Find<PageType>(SiteManager.PageTypes, "ID", page.typeID).Name;

                output.AddAttribute(HtmlTextWriterAttribute.Id, itemID);
                output.AddAttribute(HtmlTextWriterAttribute.Class, itemCategory);
                output.RenderBeginTag(HtmlTextWriterTag.Li);

                if (!string.IsNullOrEmpty(itemImgUrl))
                {
                    output.AddAttribute(HtmlTextWriterAttribute.Href, itemUrl);
                    output.AddAttribute(HtmlTextWriterAttribute.Target, Target);
                    output.AddAttribute(HtmlTextWriterAttribute.Rel, itemCategory);
                    output.AddAttribute(HtmlTextWriterAttribute.Title, itemTitle);
                    output.RenderBeginTag(HtmlTextWriterTag.A);

                    output.AddAttribute(HtmlTextWriterAttribute.Src, itemImgUrl);
                    output.AddAttribute(HtmlTextWriterAttribute.Alt, itemTitle);
                    output.RenderBeginTag(HtmlTextWriterTag.Img);
                    output.RenderEndTag();

                    output.RenderEndTag();
                }

                if (!string.IsNullOrEmpty(itemImgUrl))
                {
                    output.RenderBeginTag(HtmlTextWriterTag.P);
                    output.Write(itemTitle);
                    output.RenderEndTag();
                }
                else
                {
                    output.AddAttribute(HtmlTextWriterAttribute.Href, itemUrl);
                    output.AddAttribute(HtmlTextWriterAttribute.Target, Target);
                    output.AddAttribute(HtmlTextWriterAttribute.Rel, itemCategory);
                    output.AddAttribute(HtmlTextWriterAttribute.Title, itemTitle);
                    output.RenderBeginTag(HtmlTextWriterTag.A);

                    output.RenderBeginTag(HtmlTextWriterTag.P);
                    output.Write(itemTitle);
                    output.RenderEndTag();

                    output.RenderEndTag();
                }

                output.RenderEndTag();
                i++;
            }

            output.RenderEndTag();
        }

        private void generateListView(HtmlTextWriter output)
        {
            int i = 1;
            foreach (PageItem page in Pages)
            {
                if (ItemCount != 0 && i - 1 == ItemCount)
                    break;

                List<SiteUnity.Layers.Entity.PageItems.Contents.Content> pageContents = ContentManager.getContentByPage(page);

                if (string.IsNullOrEmpty(page.ImageUrl))
                {
                    List<File> pageImages = FileManager.getFileByPage(page);

                    File img_960x360 = null;
                    foreach (File image in pageImages)
                    {
                        if (image.TypeID == FileType.img_960x360)
                        {
                            img_960x360 = image;
                            break;
                        }
                    }

                    page.ImageUrl = img_960x360 != null ? img_960x360.Url : string.Empty;
                }

                string itemID = "id" + i;
                string itemTitle = page.Title;
                string itemUrl = page.Url;
                string itemImgUrl = page.ImageUrl;
                string itemCategory = ClassCollection.Find<PageType>(SiteManager.PageTypes, "ID", page.typeID).Name;

                output.AddAttribute(HtmlTextWriterAttribute.Id, itemID);
                output.AddAttribute(HtmlTextWriterAttribute.Class, View.ToString());
                output.RenderBeginTag(HtmlTextWriterTag.Div);



                output.AddAttribute(HtmlTextWriterAttribute.Class, itemCategory);
                output.RenderBeginTag(HtmlTextWriterTag.H2);

                output.RenderBeginTag(HtmlTextWriterTag.Span);
                output.Write(Formatter.Date(page.Date));
                output.RenderEndTag();

                output.AddAttribute(HtmlTextWriterAttribute.Href, itemUrl);
                output.AddAttribute(HtmlTextWriterAttribute.Target, Target);
                output.AddAttribute(HtmlTextWriterAttribute.Rel, itemCategory);
                output.AddAttribute(HtmlTextWriterAttribute.Title, string.Empty);
                output.RenderBeginTag(HtmlTextWriterTag.A);
                output.Write(itemTitle);
                output.RenderEndTag();

                output.RenderEndTag();

                if (!string.IsNullOrEmpty(itemImgUrl))
                {
                    output.RenderBeginTag(HtmlTextWriterTag.P);

                    //output.RenderBeginTag(HtmlTextWriterTag.Span);
                    //output.Write(Formatter.Date(page.Date));
                    //output.RenderEndTag();

                    output.AddAttribute(HtmlTextWriterAttribute.Href, itemUrl);
                    output.AddAttribute(HtmlTextWriterAttribute.Target, Target);
                    output.AddAttribute(HtmlTextWriterAttribute.Rel, itemCategory);
                    output.AddAttribute(HtmlTextWriterAttribute.Title, string.Empty);
                    output.RenderBeginTag(HtmlTextWriterTag.A);

                    output.AddAttribute(HtmlTextWriterAttribute.Src, itemImgUrl);
                    output.AddAttribute(HtmlTextWriterAttribute.Alt, string.Empty);
                    output.RenderBeginTag(HtmlTextWriterTag.Img);
                    output.RenderEndTag();

                    output.RenderEndTag();

                    output.RenderEndTag();
                }

                if (pageContents.Count != 0)
                {
                    foreach (SiteUnity.Layers.Entity.PageItems.Contents.Content content in pageContents)
                    {
                        if (content.TypeID == Layers.Entity.PageItems.Contents.ContentType.ShortText)
                        {
                            output.RenderBeginTag(HtmlTextWriterTag.P);
                            output.Write(content.ContentText);
                            output.RenderEndTag();
                        }
                    }
                }

                output.RenderEndTag();
                i++;
            }
        }

        private void generateDefaultView(HtmlTextWriter output)
        {
            int i = 1;
            foreach (PageItem page in Pages)
            {
                if (ItemCount != 0 && i - 1 == ItemCount)
                    break;

                string itemID = "id" + i;
                string itemTitle = page.Title;
                string itemUrl = page.Url;
                string itemCategory = ClassCollection.Find<PageType>(SiteManager.PageTypes, "ID", page.typeID).Name;

                output.AddAttribute(HtmlTextWriterAttribute.Id, itemID);
                output.AddAttribute(HtmlTextWriterAttribute.Class, View.ToString());
                output.RenderBeginTag(HtmlTextWriterTag.Div);

                output.AddAttribute(HtmlTextWriterAttribute.Class, itemCategory);
                output.RenderBeginTag(HtmlTextWriterTag.P);

                output.AddAttribute(HtmlTextWriterAttribute.Href, itemUrl);
                output.AddAttribute(HtmlTextWriterAttribute.Target, Target);
                output.AddAttribute(HtmlTextWriterAttribute.Rel, itemCategory);
                output.AddAttribute(HtmlTextWriterAttribute.Title, string.Empty);
                output.RenderBeginTag(HtmlTextWriterTag.A);
                output.Write(itemTitle);
                output.RenderEndTag();

                output.RenderBeginTag(HtmlTextWriterTag.Span);
                output.Write(Formatter.Date(page.Date));
                output.RenderEndTag();

                output.RenderEndTag();
                output.RenderEndTag();
                i++;
            }
        }

        protected override void RenderContents(HtmlTextWriter output)
        {
            if (!hasPages)
                return;

            StringBuilder sb = new StringBuilder();
            HtmlTextWriter htw = new HtmlTextWriter(new System.IO.StringWriter(sb, CultureInfo.InvariantCulture));

            if (ShowHeader)
            {
                output.RenderBeginTag(HtmlTextWriterTag.H2);
                output.Write(HeaderText);
                output.RenderEndTag();
            }

            switch (View)
            {
                case View.Grid:
                    generateGridView(htw);
                    break;
                case View.List:
                    generateListView(htw);
                    break;
                case View.Default:
                    generateDefaultView(htw);
                    break;
            }

            output.Write(sb.ToString());
        }
    }
}
